package com.dao;
import java.sql.*;

public class FlightJdbc {
    Connection conn;
    Statement stem;
    ResultSet rs;
    public void lianjie(){
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://120.76.45.216:3306/buy_ticket","root","root");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public ResultSet show(){
        this.lianjie();
        try{
            stem = conn.createStatement();
            rs = stem.executeQuery("select  * from flight");
        }catch (SQLException e){
            e.printStackTrace();
        }
        return rs;
    }
    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public ResultSet selectById(int id){
        this.lianjie();
        try{
            stem=conn.createStatement();
            rs=stem.executeQuery("select * from flight where id = '"+id+"'");
        }catch (SQLException e){
            e.printStackTrace();
        }
        return rs;
    }
    public void add(String num,String splace,String splaces,String stime,String etime,String mprice,String seat,String flightnum){
        this.lianjie();
        try{
            stem=conn.createStatement();
            stem.executeUpdate("insert into flight values(0, '"+num+"'  ,'"+splace+"'  , '"+splaces+"'  ,'"+stime+"'  , '"+etime+"' , '"+mprice+"' , '"+seat+"', '"+flightnum+"')");
        }catch (SQLException e){
            e.printStackTrace();
        }
        this.close();
    }
    public void delete(String id) {
        this.lianjie();
        try {
            stem=conn.createStatement();
            stem.executeUpdate("delete from flight where id = '"+id+"'");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        this.close();
    }
    public void update(String id,String num,String splace,String splaces,String stime,String etime,String mprice,String seat,String flightnum) {
        this.lianjie();
        try {
            stem=conn.createStatement();
            stem.executeUpdate("update flight set num='"+num+"'  ,splace='"+splace+"'  ,splaces='"+splaces+"', stime='"+stime+"'  , etime='"+etime+"'  , mprice='"+mprice+"'  , seat='"+seat+"'  , flightnum='"+flightnum+"'where id='"+id+"'");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        this.close();
    }
    public void close(){
        try{
            if (conn!=null){
                conn.close();
            }
            if (stem!=null){
                stem.close();
            }
            if (rs!=null){
                rs.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}














